"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
// const fs = require("fs");
const fs = require("fs");
const path = require("path");
const DateUtil_1 = require("./DateUtil");
class LogErrorFileUtil {
    static mkdirsSync(dirname) {
        if (fs.existsSync(dirname)) {
            return true;
        }
        else {
            if (this.mkdirsSync(path.dirname(dirname))) {
                fs.mkdirSync(dirname);
                return true;
            }
        }
    }
    static init() {
        if (this.canLog && !this.isInited) {
            this.isInited = true;
            this.Log_Path = path.resolve() + "/output/logs";
            console.log("Log_Path=====", this.Log_Path);
            this.mkdirsSync(this.Log_Path);
        }
    }
    static writeLog(logStr, folderName, fileName) {
        if (!fs.existsSync(folderName)) {
            fs.mkdirSync(folderName);
        }
        let filedir = path.join(folderName, fileName);
        let buf = Buffer.from("");
        if (fs.existsSync(filedir)) {
            buf = fs.readFileSync(filedir);
        }
        let tempBuf = Buffer.from(logStr + "\n");
        let newBuff;
        if (buf != null) {
            newBuff = Buffer.concat([buf, tempBuf], tempBuf.length + buf.length);
        }
        else {
            newBuff = tempBuf;
        }
        fs.writeFileSync(filedir, newBuff);
    }
    static debug(str) {
        if (this.canLog) {
            this.init();
            let fileName = this.Log_File_Name_List[0 /* DEBUG */] + "-" + DateUtil_1.DateUtil.formatDateStr3(DateUtil_1.DateUtil.now()) + this.Ext_Name;
            this.writeLog(str, this.Log_Path, fileName);
        }
    }
    static log(str) {
        if (this.canLog) {
            this.init();
            let fileName = this.Log_File_Name_List[1 /* LOG */] + "-" + DateUtil_1.DateUtil.formatDateStr3(DateUtil_1.DateUtil.now()) + this.Ext_Name;
            this.writeLog(str, this.Log_Path, fileName);
        }
    }
    static info(str) {
        if (this.canLog) {
            this.init();
            let fileName = this.Log_File_Name_List[2 /* INFO */] + "-" + DateUtil_1.DateUtil.formatDateStr3(DateUtil_1.DateUtil.now()) + this.Ext_Name;
            this.writeLog(str, this.Log_Path, fileName);
        }
    }
    static warn(str) {
        if (this.canLog) {
            this.init();
            let fileName = this.Log_File_Name_List[3 /* WARN */] + "-" + DateUtil_1.DateUtil.formatDateStr3(DateUtil_1.DateUtil.now()) + this.Ext_Name;
            this.writeLog(str, this.Log_Path, fileName);
        }
    }
    static error(str) {
        if (this.canLog) {
            this.init();
            let fileName = this.Log_File_Name_List[4 /* ERROR */] + "-" + DateUtil_1.DateUtil.formatDateStr3(DateUtil_1.DateUtil.now()) + this.Ext_Name;
            this.writeLog(str, this.Log_Path, fileName);
        }
    }
    static clientLog(str) {
        if (this.canLog) {
            this.init();
            let fileName = "clientLog" + "-" + DateUtil_1.DateUtil.formatDateStr3(DateUtil_1.DateUtil.now()) + this.Ext_Name;
            this.writeLog(str, this.Log_Path, fileName);
        }
    }
}
exports.LogErrorFileUtil = LogErrorFileUtil;
LogErrorFileUtil.Log_File_Name_List = [
    "debug",
    "log",
    "info",
    "warn",
    "error",
    "clientLog"
];
LogErrorFileUtil.Ext_Name = ".txt";
LogErrorFileUtil.canLog = true;
LogErrorFileUtil.isInited = false;
